| Conditions | 1 |
| Paths | 2304 |
| Total Lines | 156 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /** |
||
| 24 | var et2_timestamper = (function(){ "use strict"; return et2_button.extend([], |
||
| 25 | { |
||
| 26 | attributes: { |
||
| 27 | target: { |
||
| 28 | name: "Target field", |
||
| 29 | type: "string", |
||
| 30 | default: et2_no_init, |
||
| 31 | description: "Which field to place the timestamp in" |
||
| 32 | }, |
||
| 33 | image: { |
||
| 34 | default: "timestamp" |
||
| 35 | }, |
||
| 36 | background_image: { |
||
| 37 | default: true |
||
| 38 | } |
||
| 39 | }, |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Constructor |
||
| 43 | * |
||
| 44 | * @memberOf et2_button |
||
| 45 | */ |
||
| 46 | init: function() { |
||
| 47 | this._super.apply(this, arguments); |
||
| 48 | }, |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Overwritten to maintain an internal clicked attribute |
||
| 52 | * |
||
| 53 | * @param _ev |
||
| 54 | * @returns {Boolean} |
||
| 55 | */ |
||
| 56 | click: function(_ev) { |
||
| 57 | // ignore click on readonly button |
||
| 58 | if (this.options.readonly) return false; |
||
| 59 | |||
| 60 | this._insert_text(); |
||
| 61 | |||
| 62 | return false; |
||
| 63 | }, |
||
| 64 | |||
| 65 | _insert_text: function() { |
||
| 66 | var text = ""; |
||
| 67 | var now = new Date(); |
||
| 68 | text += date(egw.preference('dateformat') + ' ' + (egw.preference("timeformat") === "12" ? "h:ia" : "H:i")+' ',now); |
||
| 69 | |||
| 70 | // Get properly formatted user name |
||
| 71 | var user = parseInt(egw.user('account_id')); |
||
| 72 | var accounts = egw.accounts('accounts'); |
||
| 73 | for(var j = 0; j < accounts.length; j++) |
||
| 74 | { |
||
| 75 | if(accounts[j].value === user) |
||
| 76 | { |
||
| 77 | text += accounts[j].label; |
||
| 78 | break; |
||
| 79 | } |
||
| 80 | } |
||
| 81 | text += ': '; |
||
| 82 | |||
| 83 | var widget = this._get_input(this.target); |
||
| 84 | var input = widget.input ? widget.input : widget.getDOMNode(); |
||
| 85 | if(input.context) |
||
| 86 | { |
||
| 87 | input = input.get(0); |
||
| 88 | } |
||
| 89 | |||
| 90 | var scrollPos = input.scrollTop; |
||
| 91 | var browser = ((input.selectionStart || input.selectionStart == "0") ? |
||
| 92 | "standards" : (document.selection ? "ie" : false ) ); |
||
| 93 | |||
| 94 | var pos = 0; |
||
| 95 | var CK = CKEDITOR && CKEDITOR.instances[input.id] || false; |
||
| 96 | |||
| 97 | // Find cursor or selection |
||
| 98 | if (browser == "ie") |
||
| 99 | { |
||
| 100 | input.focus(); |
||
| 101 | var range = document.selection.createRange(); |
||
| 102 | range.moveStart ("character", -input.value.length); |
||
| 103 | pos = range.text.length; |
||
| 104 | } |
||
| 105 | else if (browser == "standards") |
||
| 106 | { |
||
| 107 | pos = input.selectionStart; |
||
| 108 | }; |
||
| 109 | |||
| 110 | // If CKEDitor, update it |
||
| 111 | if(CKEDITOR && CKEDITOR.instances[input.id]) |
||
| 112 | { |
||
| 113 | CKEDITOR.instances[input.id].insertText(text); |
||
| 114 | window.setTimeout(function() { |
||
| 115 | CKEDITOR.instances[input.id].focus(); |
||
| 116 | }, 10); |
||
| 117 | } |
||
| 118 | else |
||
| 119 | { |
||
| 120 | // Insert the text |
||
| 121 | var front = (input.value).substring(0, pos); |
||
| 122 | var back = (input.value).substring(pos, input.value.length); |
||
| 123 | input.value = front+text+back; |
||
| 124 | |||
| 125 | // Clean up a little |
||
| 126 | pos = pos + text.length; |
||
| 127 | if (browser == "ie") { |
||
| 128 | input.focus(); |
||
| 129 | var range = document.selection.createRange(); |
||
| 130 | range.moveStart ("character", -input.value.length); |
||
| 131 | range.moveStart ("character", pos); |
||
| 132 | range.moveEnd ("character", 0); |
||
| 133 | range.select(); |
||
| 134 | } |
||
| 135 | else if (browser == "standards") { |
||
| 136 | input.selectionStart = pos; |
||
| 137 | input.selectionEnd = pos; |
||
| 138 | input.focus(); |
||
| 139 | } |
||
| 140 | input.scrollTop = scrollPos; |
||
| 141 | input.focus(); |
||
| 142 | } |
||
| 143 | // If on a tab, switch to that tab so user can see it |
||
| 144 | var tab = widget; |
||
| 145 | while(tab._parent && tab._type != 'tabbox') |
||
| 146 | { |
||
| 147 | tab = tab._parent; |
||
| 148 | } |
||
| 149 | if (tab._type == 'tabbox') tab.activateTab(widget); |
||
| 150 | }, |
||
| 151 | |||
| 152 | _get_input: function _get_input(target) |
||
| 153 | { |
||
| 154 | var input = null; |
||
| 155 | var widget = null; |
||
| 156 | |||
| 157 | if (typeof target == 'string') |
||
| 158 | { |
||
| 159 | widget = this.getRoot().getWidgetById(target); |
||
| 160 | } |
||
| 161 | else if (target.instanceOf && target.instanceOf(et2_IInput)) |
||
| 162 | { |
||
| 163 | widget = target; |
||
| 164 | } |
||
| 165 | else if(typeof target == 'string' && target.indexOf('#') < 0 && jQuery('#'+this.target).is('input')) |
||
| 166 | { |
||
| 167 | input = this.target; |
||
| 168 | } |
||
| 169 | if(widget) |
||
| 170 | { |
||
| 171 | return widget; |
||
| 172 | } |
||
| 173 | if(input.context) |
||
| 174 | { |
||
| 175 | input = input.get(0); |
||
| 176 | } |
||
| 177 | return input; |
||
| 178 | } |
||
| 179 | });}).call(this); |
||
| 180 | et2_register_widget(et2_timestamper, ["button-timestamp", "timestamper"]); |